//
//  ZH_HomeVedioInfoViewController.m
//  WisdomStar
//
//  Created by zhangliwen on 2018/10/25.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_HomeVedioInfoViewController.h"
#import "ZH_HomeVedioTableViewCell.h"
#import "ZH_ClassViewModel.h"
#import "ZH_HomeSourceInfoViewController.h"
@interface ZH_HomeVedioInfoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,weak)UITableView *tableView;
@property (nonatomic,strong)ZH_ClassViewModel *viewModel;
@property (nonatomic,assign)NSInteger page;
@end

@implementation ZH_HomeVedioInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUserInterface];
    [self loadTableView];
    [self loadDataForStart:1 isUpMore:NO];
}
- (void)cleanUpData{
    [self setViewModel:nil];
}
- (void)initUserInterface{
    self.view.backgroundColor = kCommonBackgroudColor;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(FITSCALE(9));
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-[DATAMODEL KNavigationBarHeioght] - FITSCALE(60));
    }];
    
}
///加载列表数据
- (void)loadTableView{
    WEAKSelf;
    //上拉加载更多
    MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        [weakSelf loadDataForStart:self.page isUpMore:YES];
    }];
    
    [footer setTitle:HenLocalizedString(@"没有更多的信息！") forState:MJRefreshStateNoMoreData];
    self.tableView.mj_footer = footer;
}
-(void)loadDataForStart:(NSInteger)start isUpMore:(BOOL)isUpMore{
    [self.viewModel.courseListParam setObject:self.cateString forKey:@"cate"];
    [self.viewModel.courseListParam setObject:self.gradeString forKey:@"grade"];
    [self.viewModel.courseListParam setObject:self.typeString forKey:@"way"];
    [self.viewModel.courseListParam setObject:[NSString stringWithFormat:@"%ld",(long)start] forKey:@"page"];
    WEAKSelf;
    [self.viewModel courseListResultBlock:^(NSString *code, NSString *message, id data) {
        [weakSelf hideHud];
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
        if(isUpMore){
            if(((NSMutableArray*)data).count < [NumberOfPages integerValue]){
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        if ([code isEqualToString:@"0"]) {
            [weakSelf.tableView reloadData];
        }
        else{
            if ([code isEqualToString:@"1002"]) {
                [self showHint:@"登录过期"];
            }
            else{
                [weakSelf showHint:message];
            }
        }
    }];
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.viewModel.courseListArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return FITSCALE(40);
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [UIView createViewWithBackgroundColor:kFontColorWhite];
    UIView *lineView = [UIView createViewWithBackgroundColor:[UIColor colorWithHexString:@"#00C9AE"]];
    [view addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view);
        make.width.mas_equalTo(FITSCALE(4));
        make.height.mas_equalTo(FITSCALE(18));
        make.left.equalTo(view).offset(FITSCALE(11));
    }];
    UILabel *label = [UILabel createLabelWithText:@"精品课程" font:kFontSize_30];
    label.font = [UIFont boldSystemFontOfSize:15];
    [view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view);
        make.left.equalTo(lineView.mas_right).offset(FITSCALE(8));
    }];
    UIView *lineViews = [UIView createViewWithBackgroundColor:[UIColor colorWithHexString:@"#DFDFDF"]];
    [view addSubview:lineViews];
    [lineViews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(view);
        make.height.mas_equalTo(FITSCALE(1));
    }];
    return view;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZH_HomeVedioTableViewCell *cell = [ZH_HomeVedioTableViewCell cellWithTableView:tableView];
    [cell updataUI:self.viewModel.courseListArray[indexPath.row]];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ZH_HomeSourceInfoViewController *vc = [[ZH_HomeSourceInfoViewController alloc] init];
    vc.idString = [NSString stringWithFormat:@"%@",self.viewModel.courseListArray[indexPath.row][@"id"]];
    vc.typeStrings = @"2";
    vc.dics = self.viewModel.courseListArray[indexPath.row];
    vc.nameString = [NSString stringWithFormat:@"%@",self.viewModel.courseListArray[indexPath.row][@"name"]];
    vc.priceString = [NSString stringWithFormat:@"%@",self.viewModel.courseListArray[indexPath.row][@"price"]];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - getter,setter
- (UITableView *)tableView{
    if (!_tableView) {
        UITableView *tableView = [UITableView createTableViewWithDelegateTarget:self];
        tableView.backgroundColor = kCommonBackgroudColor;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [tableView setCellAutoAdaptationForEstimatedRowHeight:FITSCALE(100)];
        [self.view addSubview:_tableView = tableView];
    }
    return _tableView;
}

- (ZH_ClassViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[ZH_ClassViewModel alloc] init];
    }
    return _viewModel;
}

@end
